package com.developer.platform.application.query;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Method;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.developer.platform.application.query.assembler.LogAssembler;
import com.developer.platform.application.query.dto.LogDto;
import com.developer.platform.application.query.dto.ResponseModel;
import com.developer.platform.domain.Log;
import com.developer.platform.infrastructure.data.mapper.LogMapper;
import com.developer.platform.infrastructure.enums.StatusEnum;
import com.veeker.core.bean.Pageable;
import java.util.List;
import java.util.Objects;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2021-01-07
 */
@Component
public class LogQueryManager {

  private final LogMapper logMapper;
  private final LogAssembler logAssembler;

  public LogQueryManager(
      LogMapper logMapper,
      LogAssembler logAssembler) {
    this.logMapper = logMapper;
    this.logAssembler = logAssembler;
  }

  public ResponseModel<LogDto> list(Long userId, String methodName, String businessType, Method method,
      StatusEnum status, String discription, Pageable pageable){
    LambdaQueryWrapper<Log> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Objects.nonNull(method), Log::getMethod,method);
    queryWrapper.eq(Objects.nonNull(status), Log::getStatus,status);
    queryWrapper.eq(StrUtil.isNotBlank(businessType), Log::getBusinessType,businessType);
    queryWrapper.eq(StrUtil.isNotBlank(methodName), Log::getMethodName,methodName);
    queryWrapper.eq(StrUtil.isNotBlank(discription), Log::getDiscription,discription);
    queryWrapper.eq(Objects.nonNull(userId), Log::getCreateBy,userId);
    queryWrapper.orderByDesc(Log::getCreateTime);
    return logAssembler.toLogDto(logMapper
        .selectPage(new Page<>(pageable.getPageNumber(), pageable.getPageSize()),
            queryWrapper));
  }

  public List<LogDto> list(){
    LambdaQueryWrapper<Log> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.notLike(Log::getMethod,"GET");
    queryWrapper.eq(Log::getStatus, StatusEnum.Y);
    queryWrapper.orderByDesc(Log::getCreateTime);
    return logAssembler.toLogDto(logMapper
        .selectPage(new Page<>(1,30),
            queryWrapper).getRecords());
  }

}
